/**
 * Created by Administrator on 2017/5/24.
 */
var express=require('express');
var service=require('../service/userService');
var userRouter=express.Router();

userRouter.post('/save',function(req,res,next){
    var doc = req.body;
    if(doc.id!=''){
        service.update(doc,function (msg) {
            if(msg=="ok"){
                //res.send("修改成功");
            }else{
                //res.send("修改失败");
            }
            res.redirect("/user/sel");
        })
    }else{
        //modeluser添加方法
        //var addDoc = {name:doc.name,password:doc.pwd}
        delete doc.id;
        service.save(doc,function (msg) {
            if(msg=="ok"){
                //res.send("添加成功");
            }else{
                //res.send("添加失败");
            }
            res.redirect("/user/sel");
        })
    }

});
userRouter.get('/sel',function (req,res,next) {
    var doc = {};
    var page = req.query.page;
    var rows = req.query.rows;
    if(page==undefined){
        doc.page = 1;
    }else{
        doc.page = page;
    }
    var page = doc.page;
    if(rows==undefined){
        doc.rows = 2;
    }else{
        doc.rows = rows;
    }
    var rows = doc.rows;
    service.find(doc,function (doc) {
        res.render("index",{title:'demo首页',items:doc,page:page,rows:rows,total:Math.ceil(doc.total/rows)});
    });
});
userRouter.get('/del',function(req,res,next){
    var id = req.query.id;
    service.del(id,function(msg){
        if(msg=="ok"){
        }else{
        }
        res.redirect("/user/sel");
    })
});
userRouter.get('/edit/:id',function (req,res,next) {
    var id = req.params.id;
    service.findById(id,function (doc) {
        res.render("edit",{title:'修改',user:doc});
    });
});
module.exports=userRouter;